package com.rrd.hive.util;

import java.io.UnsupportedEncodingException;

public class StringPadUtil {

    public static String rightPad(String s, int n, String flag)
            throws UnsupportedEncodingException {
        StringBuilder sb = new StringBuilder();
        if (s == null) {
            s = " ";
        }
        byte[] srcBytes = s.getBytes("GB18030");
//		byte[] desBytes = new byte[n];
        if (srcBytes.length == n) {
            return s;
        } else if (srcBytes.length > n) {
            //进行截取字符串
//			System.arraycopy(srcBytes, 0, desBytes, 0, n);
//			s = new String(desBytes, "GB18030");
//			return s;
            s = subByteArr(s, n);
            srcBytes = s.getBytes("GB18030");
        }
        sb.append(s);
        for (int i = 0; i < n - srcBytes.length; i++) {
            sb.append(flag);
        }
        return sb.toString();
    }

    public static String rightPad(String s, int n)
            throws UnsupportedEncodingException {
        return rightPad(s, n, " ");
    }

    public static String leftPad(String s, int n, String flag)
            throws UnsupportedEncodingException {
        StringBuilder sb = new StringBuilder();
        if (s == null || s.equals("")) {
        	return padBlack(n);
        }
        byte[] srcBytes = s.getBytes("GB18030");
//		byte[] desBytes = new byte[n];
        if (srcBytes.length == n) {
            return s;
        } else if (srcBytes.length > n) {
            //进行截取字符串
//			System.arraycopy(srcBytes, 0, desBytes, 0, n);
//			s = new String(desBytes, "GB18030");
            s = subByteArr(s, n);
            srcBytes = s.getBytes("GB18030");
        }
        for (int i = 0; i < n - srcBytes.length; i++) {
            sb.append(flag);
        }
        sb.append(s);
        return sb.toString();
    }
    public static String leftPad2(String s, int n, String flag)
            throws UnsupportedEncodingException {
        StringBuilder sb = new StringBuilder();
        if (s == null || s.equals("")) {
            s = "0";
        }
        byte[] srcBytes = s.getBytes("GB18030");
//		byte[] desBytes = new byte[n];
        if (srcBytes.length == n) {
            return s;
        } else if (srcBytes.length > n) {
            //进行截取字符串
//			System.arraycopy(srcBytes, 0, desBytes, 0, n);
//			s = new String(desBytes, "GB18030");
            s = subByteArr(s, n);
            srcBytes = s.getBytes("GB18030");
        }
        for (int i = 0; i < n - srcBytes.length; i++) {
            sb.append(flag);
        }
        sb.append(s);
        return sb.toString();
    }
    private static String padBlack(int n){
    	StringBuilder sb = new StringBuilder();
    	for(int i=0;i<n;i++){
    		sb.append(" ");
    	}
    	return sb.toString();
    }

    public static String leftPad(String s, int n)
            throws UnsupportedEncodingException {
        return leftPad(s, n, "0");
    }

    private static String subByteArr(String s, int n)
            throws UnsupportedEncodingException {
        int i = s.length();
        while (s.substring(0, i).getBytes("GB18030").length > n) {
            i--;
        }
        return s.substring(0, i);
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
//        String s = "AN10551000H002191                                                            HHL3958985                              1101052019102320200702CNY0000000450000000045000000004504039  7  2  53        201912022019120200000000530000000053000000035300000000000000000000000000000000000000000000000000000000011/////////////////////*NN1杨勇勇                        0610221199207125618                              E友勇信                        061088770086278417300000004501";
        String s="唐努尔·托留别克";
        System.out.println(s.getBytes("GB18030").length);
        s="唐努尔-托留别克";
        System.out.println(s.getBytes("GB18030").length);
//        System.out.println(subByteArr(s,403));
//        System.out.println(subByteArr(s,404));
    }
}